package com.shuangyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuangyu.common.R;
import com.shuangyu.entity.User;
import com.shuangyu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R<String> login(HttpServletRequest request, @RequestBody User user){

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, user.getPhone());
        User userInfo = userService.getOne(queryWrapper);

        if (userInfo == null){
            return R.error("没有该账号");
        }

        if (!userInfo.getPassword().equals(user.getPassword())){
            return R.error("账号密码错误");
        }

        request.getSession().setAttribute("user", userInfo.getId());
        request.getSession().setAttribute("phone", userInfo.getPhone());

        return R.success("登录成功");
    }
}
